﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web.Security;
using System.Web;
using Sahit.iSoilHotel.Portal.ServiceProxy.TavernAuthenticationServiceReference;

namespace Sahit.iSoilHotel.Portal.ServiceProxy.Tavern.Proxys.Authentication
{
    /// <summary>
    /// A Proxy API for Tavern
    /// </summary>
    public class TavernAuthenticationServiceProxy : ITavernAuthenticationServiceProxy
    {
        private TavernAuthenticationServiceClient tavernService;
        private HttpContextBase httpContext;
        private AccountDTO cachedAccount;

        public TavernAuthenticationServiceProxy(HttpContextBase httpContext)
        {
            this.httpContext = httpContext;
        }
        /// <summary>
        /// Validate an account by user's name and password
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool ValidateAccount(string userName, string password)
        {
            using (tavernService = new TavernAuthenticationServiceClient())
            {
                bool isValid = tavernService.ValidateAccount(userName, password);
                tavernService.Close();
                return isValid;
            }
        }

        /// <summary>
        /// Sign in a validated account
        /// </summary>
        /// <param name="userName"></param>
        public void SignIn(string userName)
        {
            AccountDTO account;
            DateTime currentDateTime = DateTime.UtcNow.ToLocalTime();

            using (tavernService = new TavernAuthenticationServiceClient())
            {
                account = tavernService.GetAccountByUserName(userName);
                tavernService.Close();
            }

            var ticket = new FormsAuthenticationTicket(
                1,
                account.Name,
                currentDateTime,
                currentDateTime.AddDays(30),
                true,
                account.Name,
                FormsAuthentication.FormsCookiePath);

            var encryptedTicket = FormsAuthentication.Encrypt(ticket);

            var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
            cookie.HttpOnly = true;
            if (ticket.IsPersistent)
            {
                cookie.Expires = ticket.Expiration;
            }
            cookie.Secure = FormsAuthentication.RequireSSL;
            cookie.Path = FormsAuthentication.FormsCookiePath;
            if (FormsAuthentication.CookieDomain != null)
            {
                cookie.Domain = FormsAuthentication.CookieDomain;
            }
            httpContext.Response.Cookies.Add(cookie);
            cachedAccount = account;
        }

        /// <summary>
        /// Register a new account
        /// </summary>
        /// <param name="registrationRequest"></param>
        /// <returns></returns>
        public bool RegisterAccount(RegistrationRequestDTO registrationRequest)
        {
            using (tavernService = new TavernAuthenticationServiceClient())
            {
                RegistrationResultDTO registrationResult = tavernService.RegisterAccount(registrationRequest);
                tavernService.Close();
                return true;
            }
        }


        public void SignOut()
        {
            FormsAuthentication.SignOut();
            cachedAccount = null;
        }
    }
}
